#include <func.h>

void dirent_display(struct dirent* p)
{
    printf("d_ino = %lld, d_off = %ld, d_reclen = %hu, d_type = %d, d_name = %s\n",
           (long long)p->d_ino,
           p->d_off,
           p->d_reclen, 
           p->d_type,
           p->d_name);
}

int main(int argc, char* argv[])
{
    // 遍历目录下的每一个目录项
    // 打印每一个 struct dirent 的信息
    // ./t_readdir dir 
    if (argc != 2) {
        fprintf(stderr, "Usage: %s dir\n", argv[0]);
        exit(1);
    }

    // 1. 打开目录流
    DIR* stream = opendir(argv[1]);
    if (!stream) {
        perror("opendir()");
        exit(1);
    }
    // 2. 读取每一个目录项
    errno = 0;
    struct dirent* curr;
    while ((curr = readdir(stream)) != NULL) {
        dirent_display(curr);
    } // curr == NULL

    if (errno) {
        perror("readdir()");
        exit(1);
    }
    // 3. 关闭目录流
    closedir(stream);
    return 0;
}

